// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, { soundList } from "../../framework/AudioManage";
import GameConfig from "../../framework/GameConfig";
import mainManage from "../../framework/mainManage";
import sceneMediator from "../../framework/sceneMediator";
import Tool from "../../framework/Tool";
import gameData from "../gameData";
import { SDKManager } from "../SDKManager/SDKManager";
import XFManager from "../XFManager";

const {ccclass, property} = cc._decorator;
@ccclass
export default class gameOverMediator extends cc.Component {

    @property({displayName : "是否是胜利界面"})
    winView : boolean = false;

    @property({type : cc.Node,visible() {
        return this.winView;
    }})
    nextNode : cc.Node = null;

    @property({type : cc.Node,visible() {
        return this.winView;
    }})
    backNode : cc.Node = null;

    @property({type : cc.Node,visible() {
        return this.winView;
    }})
    pumpkinNode : cc.Node = null;

    @property({type : cc.Node,visible() {
        return this.winView;
    }})
    rewardNode : cc.Node = null;

    @property({type : cc.Node,visible() {
        return this.winView;
    }})
    handNode: cc.Node = null;

    seeVideoTimes = false;

    onLoad(){

        gameData.openTimesSwtich = false;

        if(this.winView){
            gameData.failGameData = 0;
            gameData.titleFreeTimes = 2;
            this.scheduleOnce(()=>{
                this.nextNode.active = true;
            },2);
        }else{
            gameData.failGameData++;
        }
    }

    loadPetSkin(node,name){
        cc.loader.loadRes("anim/" + name,sp.SkeletonData,(error, resource) =>{
            let spk = node.getComponent(sp.Skeleton);
            spk.skeletonData = resource;
            node.active = true;
            if(this.winView){
                spk.setAnimation(0,"daiji",true);
            }else{
                spk.setAnimation(0,"liulei",true);
            }

        })
    }

    sceneData = [9,19,29,39];
    isClick = false;
    isShow = false;
    clickBtnEvent(event,data){
        AudioManage.playSFX(soundList.uiClick,false);
        if(parseInt(data) == 1){

            //解锁下一关卡
            if(sceneMediator.getHighScene() == 13 || sceneMediator.getHighScene() == 31){
                sceneMediator.finishCurLevel();
                Tool.loadPrefab("mainPrefab/chooseLevelRank",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("chooseLevelRank").setCallBack(()=>{
                       
                    });
                    view.parent = this.node;
                });
            }else{
                sceneMediator.LoadNextScene();
                GameConfig.GetInstance().isLockHide = false;
            }
            console.log("下一关");
        }

        if(parseInt(data) == 2){
            gameData.isRestart = true;
            sceneMediator.LoadCurScene();
            console.log("重玩");
        }

        if(parseInt(data) == 3){

            GameConfig.GetInstance().isLockHide = false;
            let isSeeVideo = false;

            SDKManager.showRewardVideo("失败界面跳过本关",()=>{

                isSeeVideo = true;

                sceneMediator.jumpNextScene();

            },()=>{
                if(isSeeVideo){
                    mainManage.createToast("Hadiahnya akan diberikan setelah menonton video ini");
                }else{
                    mainManage.createToast("Tidak ada iklan yang tersedia saat ini");
                }
            });

            console.log("跳过本关");
        }

        if(parseInt(data) == 4){
            sceneMediator.backToHome();
            XFManager.GetScene().closeTime();
            console.log("返回主页");
        }

        if(parseInt(data) == 5){

            let isSeeVideo = false;

            SDKManager.showRewardVideo("成功界面获取苹果",()=>{

                isSeeVideo = true;

                this.rewardNode.active = false;

                gameData.addSinglePumpkin(2);
                this.pumpkinNode.getChildByName("lab").getComponent(cc.Label).string = "" + gameData.getCurPumpkinNumber();
                mainManage.createToast("Mendapatkan dua buah apel");

            },()=>{
                if(isSeeVideo){
                    mainManage.createToast("Hadiahnya akan diberikan setelah menonton video ini");
                }else{
                    mainManage.createToast("Tidak ada iklan yang tersedia saat ini");
                }
            })
        }
    }
}
